package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.ReportDtoIn;
import com.zyw.zhuangdian.pojo.out.ReportDtoOut;
import com.zyw.zhuangdian.service.ReportService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/report")
@RequiredArgsConstructor
@Slf4j
public class ReportController {
    private final ReportService reportService;

    /**
     * 新增投诉
     * @param json
     * @return
     */
    @RequestMapping(value = "addReport",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addReport(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"reason","投诉理由不能为空");
        Assert.hasKeyAndValue(json,"type","内容类型不能为空");
        Assert.hasKeyAndValue(json,"content","投诉内容不能为空");

        ReportDtoIn in = BeanConvertUtil.covertBean(json, ReportDtoIn.class);
        Resp<Void> resp = reportService.addReport(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改投诉
     * @param json
     * @return
     */
    @RequestMapping(value = "updateReport",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateReport(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"rid","主键ID不能为空");

        ReportDtoIn in = BeanConvertUtil.covertBean(json, ReportDtoIn.class);
        Resp<Void> resp = reportService.updateReport(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除投诉
     * @param json aid
     * @return
     */
    @RequestMapping(value = "deleteReport",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteReport(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"rid","主键ID不能为空");

        ReportDtoIn in = BeanConvertUtil.covertBean(json, ReportDtoIn.class);
        Resp<Void> resp = reportService.deleteReport(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出投诉
     * @param json
     * @return
     */
    @RequestMapping(value = "listReports",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listReports(@RequestBody JSONObject json){
        ReportDtoIn in = BeanConvertUtil.covertBean(json, ReportDtoIn.class);
        Resp<List<ReportDtoOut>> resp = reportService.listReports(in);
        return RespUtil.OK(resp);
    }

}
